from django.contrib.auth.models import AbstractUser
from django.db import models
from webboard import models as webboard_models

from ResourceWeb_2 import settings


# Create your models here.



class CustomUser(AbstractUser):
    '''
        客户表，继承User
    '''

    avatar_url = models.ImageField(verbose_name="用户头像保存路径", upload_to="uploads/images/avatars/",
                                   default="uploads/images/defaults/default.png", blank=True, null=True)

    class Meta:
        db_table = "auth_user"


class UserInformation(models.Model):
    """
    用户各种数据
    """
    user = models.OneToOneField(settings.AUTH_USER_MODEL, on_delete=models.CASCADE, verbose_name='用户',
                                related_name='detail_information')
    grade = models.IntegerField(default=0, verbose_name="等级")
    experience = models.IntegerField(default=0, verbose_name="当前经验")
    get_likes = models.IntegerField(default=0, verbose_name="总获赞数")
    get_comments = models.IntegerField(default=0, verbose_name="总评论数")
    get_collect = models.IntegerField(default=0, verbose_name="总收藏数")
    leave_message = models.CharField(max_length=255, verbose_name="用户留言", blank=True, null=True)


class Resource(models.Model):
    '''
        资源表
    '''
    RESOURCE_TYPE_CHOICES = (
        ('picture', '图片'),
        ('video', '视频'),
        ('text', '文本文件'),
    )
    resource_title = models.CharField(max_length=255, verbose_name="资源标题")
    resource_type = models.CharField(choices=RESOURCE_TYPE_CHOICES, max_length=7, verbose_name="资源类别")
    resource_description = models.TextField(verbose_name="资源描述")
    resource_uploader = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE, verbose_name="上传者",
                                          related_name='Resources')
    resource_download_count = models.IntegerField(default=0, verbose_name="下载次数")
    resource_tag = models.ManyToManyField(webboard_models.Tag, verbose_name="标签", related_name='resource_tag')
    resource_upload_time = models.DateTimeField(auto_now_add=True, verbose_name="上传时间")
    resource_look_count = models.IntegerField(default=0, verbose_name="观看量")
    resource_comments = models.IntegerField(default=0, verbose_name="评论量")




class ResourceRemark(models.Model):
    # 自动生成评论ID列
    content = models.TextField(help_text="评论内容")
    publishTime = models.DateTimeField(auto_now_add=True, verbose_name="发布时间")
    likesNumber = models.IntegerField(default=0, verbose_name="点赞数")
    dislikesNumber = models.IntegerField(default=0, verbose_name="被踩次数")
    resource = models.ForeignKey(Resource, on_delete=models.CASCADE, verbose_name="所属资源",
                                 related_name='remark_resources')
    UserId = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE, verbose_name="发起者",
                               related_name='remark_auther')


# 回复表
class ResourceReply(models.Model):
    content = models.TextField(verbose_name="回复内容")
    author = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE, verbose_name="回复作者")
    UserId = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE, verbose_name="回复对象",
                               related_name='ResourceReply_to')
    # reply_to_user = models.ForeignKey('self', null=True, blank=True, on_delete=models.SET_NULL, verbose_name="回复对象",
    #                                   related_name='ResourceReply_to')
    remarkId = models.ForeignKey(ResourceRemark, on_delete=models.CASCADE, related_name="replies",
                                 verbose_name="所属评论")
    resource = models.ForeignKey(Resource, on_delete=models.CASCADE, verbose_name="所属资源",
                                 related_name='reply_to_resource')
    publishTime = models.DateTimeField(auto_now_add=True, verbose_name="回复时间")
    likes_number = models.IntegerField(default=0, verbose_name="点赞数")
    dislikes_number = models.IntegerField(default=0, verbose_name="踩数")



